<template>
  <div class="app-container">
    <el-button type="primary" @click="addType">添加</el-button>
    <el-table v-loading="listLoading" :data="typePageData.records" border style="width: 100%">
      <el-table-column fixed="left" label="操作" width="150">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleUpdateShow(scope.row)">编辑</el-button>
          <el-button type="text" size="small" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="typeName" label="名称" width="200" />
      <el-table-column prop="typeDesc" label="描述" width="200" />
      <el-table-column prop="createBy" label="创建人" width="100" />
      <el-table-column prop="createTime" label="创建时间" width="160" />
      <el-table-column prop="updateBy" label="更新人" width="100" />
      <el-table-column prop="updateTime" label="更新时间" width="160" />
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next, sizes, total, jumper"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="typePageData.size"
      :total="typePageData.total"
      @current-change="fetchData"
    />
    <el-dialog title="编辑" :visible.sync="dialogFormVisible">
      <el-form ref="TypeForm" :model="formData" label-width="80px">
        <el-form-item label="名称">
          <el-input v-model="formData.typeName" />
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="formData.typeDesc" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="saveFormData">保存</el-button>
          <el-button @click="dialogFormVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getList, saveType, deleteType } from '@/api/blog/type'
import { getTargetKeyValueFromSource } from '@/utils/easyUtils'

export default {
  data() {
    return {
      dialogFormVisible: false,
      listLoading: true,
      typePageData: {
        records: [],
        size: 10,
        total: 0,
        current: 1
      },
      formData: {
        typeId: 0,
        typeName: '',
        typeDesc: ''
      }
    }
  },
  created() {
    this.fetchData(1)
  },
  methods: {
    handleSizeChange(val) {
      this.typePageData.size = val
      this.fetchData(1)
    },
    fetchData(current) {
      this.typePageData.current = current
      this.typePageData.records = []
      this.listLoading = true
      getList({}, this.typePageData)
        .then(response => {
          this.typePageData = getTargetKeyValueFromSource(this.typePageData, response.data)
          this.listLoading = false
        })
        .catch(err => {
          this.listLoading = false
          console.log(err)
        })
    },
    handleUpdateShow(row) {
      this.formData = getTargetKeyValueFromSource(this.formData, row)
      this.dialogFormVisible = true
    },
    handleDelete(row) {
      this.$confirm('确定删除？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteType({ 'typeId': row.typeId }).then(response => {
          this.fetchData(1)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    saveFormData() {
      this.formData.children = []
      saveType(this.formData).then(response => {
        this.dialogFormVisible = false
        this.fetchData(1)
      })
    },
    addType() {
      this.formData = getTargetKeyValueFromSource(this.formData, {
        typeId: 0,
        typeName: '',
        typeDesc: ''
      })
      this.dialogFormVisible = true
    }
  }
}
</script>
